package com.kotle.baselib.recyclerview;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2017/5/24.
 * 使用dataBinding的RecyclerView的Adapter
 */

public abstract class SimpleMvvmRlvAdapter<T extends ViewDataBinding> extends RecyclerView.Adapter<SimpleMvvmRlvAdapter<T>.MyHolder> {

    private int resId;

    public SimpleMvvmRlvAdapter(int layoutResId) {
        this.resId = layoutResId;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyHolder(DataBindingUtil.<T>inflate(LayoutInflater.from(parent.getContext()), this.resId, parent, false));
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        onBindViewHolder(holder.binding, position);
    }

    public abstract void onBindViewHolder(T binding, int position);

    @Override
    abstract public int getItemCount();

    class MyHolder extends RecyclerView.ViewHolder {
        T binding;

        MyHolder(T inflate) {
            super(inflate.getRoot());
            binding = inflate;
        }
    }

}
